# -*- coding: utf-8 -*-

from db.mysql import MysqlClient
import json
from common.config import *


class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "“一元任我行”交通工具意外险"

    def selectData(self, data={}):
        SQL = "SELECT * FROM `" + self.name + "`"

        self.mysql.cursor.execute(SQL)

        result = self.mysql.cursor.fetchall()

        for item in result:
            result = json.loads(item["plan"])
            data["name"] = self.name
            data["company"] = "中国人寿"
            data["days"] = "30d"
            data["people_count"] = item["max_num"]
            data["airplane_death"] = result["飞机交通工具意外伤害身故、伤残"].replace("w", "0000")
            data["passenger_death"] = result["汽车交通工具意外伤害身故、伤残"].replace("w", "0000")
            data["ship_disability"] = result["轮船交通工具意外伤害身故、伤残"].replace("w", "0000")
            # data["private_death"] = result["乘坐非营运机动车"].replace("w","0000")
            data["train_death"] = result["火车交通工具意外伤害身故、伤残"].replace("w", "0000")
            data["price"] = item["price"]

            #
            self.mergeTable(data)

    # 合并表格
    def mergeTable(self, data):
        SQL = 'INSERT INTO `' + TABLE_ACCIDENT + '`(`name`,`company`,`days`,`people_count`,`airplane_death`,`passenger_death`,`ship_disability`,`train_death`,`price`)VALUES("{name}","{company}","{days}","{people_count}","{airplane_death}","{passenger_death}","{ship_disability}","{train_death}","{price}")'

        print(SQL.format(**data))

        self.mysql.cursor.execute(SQL.format(**data))
        self.mysql._conn.commit()


if __name__ == '__main__':
    a = Arrangement()
    a.selectData()
